<?php
/**
 * Description of newsletter
 *
 * @author Gmax
 */
class modele_newsletter extends modele{
    protected static $_table = 'newsletter';
         
    private $_id = 0;
    private $_mailjet_id = 0;
    private $_title = '';
    private $_content = '';
    private $_date_crea = '0000-00-00 00:00:00';
    private $_is_deleted = 0;
    
    public static $htmlHeader = '';
    public static $htmlFooter = '';
    
    function __construct($id=null){
        if($id!==null){
            $this->_id = $id;
            $this->_constructObject();
        }
    }
    private function _constructObject(){
        $sqlQuery = 'SELECT ';
        $sqlQuery .= 't.* ';
        $sqlQuery .= 'FROM '.self::getTable().' as t ';
        $sqlQuery .= 'WHERE t.id='.$this->_id.' ';
        $sqlQuery .= 'LIMIT 1';        
        
        $datas = $this->_getOneResult($sqlQuery);
        if($datas){
            $this->_id = $datas['id'];
            $this->_mailjet_id = $datas['mailjet_id'];
            $this->_title = $datas['title'];
            $this->_content =  $datas['content'];
            $this->_date_crea =  $datas['date_crea'];
            $this->_is_deleted =  $datas['is_deleted'];
        }
    }
    
    public function getId(){
        return $this->_id;
    }
    public function getMailjetId(){
        return $this->_mailjet_id;
    }
    public function getTitle($input=false){
        if($input)return $this->inputFormat($this->_title);
        else return $this->outputFormat($this->_title);
    }
    public function getDateCrea(){
        return $this->_date_crea;
    }
    public function getIsDeleted(){
        return $this->_is_deleted;
    }
    public function isDeleted(){
        return ($this->getIsDeleted()==1)?true:false;
    }
    public function getContent($input=false){
        if($input)return $this->inputFormat($this->_content);
        else return $this->outputFormat($this->_content);
    }
    
    public function getHtmlHeader(){
        $templateHtml = '<!DOCTYPE HTML>
<html lang="fr">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>'.$this->getTitle().'</title>
<style type="text/css">
					a { border: none; }
					img { border: none; }
					p { margin: 0; line-height: 1.3em; }
					#footer-msg a { color: #F3A836; }
					h1,h2,h3,h4,h5,h6 {font-size:100%;margin:0;}
				</style>
</head>
<body style="margin: 0; padding: 0; background-color: #eeeeee" bgcolor="#eeeeee">
				
				<table cellpadding="0" cellspacing="0" border="0" align="center" width="100%"><tr>
<td align="center" style="padding: 37px 0; background-color: #eeeeee;" bgcolor="#eeeeee">
<div style="padding: 0 0 10px"><a href="[[PERMALINK]]?a=[[MAILJET_ID]]&amp;b=[[MAILJET_CHK]]&amp;email=[[EMAIL_TO]]" style="line-height: 1;vertical-align: bottom;color: #000000; font-family: arial; font-size: 11px; ;border: none;">Voir la version en ligne</a></div>
			      <!-- #nl_container -->
			      <table cellpadding="0" cellspacing="0" border="0" style="margin: 0; border: 1px solid #dddddd; color: #444444; font-family: arial; font-size: 12px; border-color: #dddddd; background-color: #ffffff; " width="600"><tr>
<td>
			            
			            <!-- #nl_header -->
			            <table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>
<td>                                    
<!-- ++++++++++++++++++++++++++++++logo+++++++++++++++++++++++++++++ -->
</td>
			              </tr></table>
<!-- #nl_content --><table cellpadding="0" cellspacing="0" border="0" style="margin: 0; border-collapse: collapse;" width="100%"><tr>
<td style="color: #444444; font-family: arial; font-size: 12px; border-color: #dddddd; background-color: #ffffff;  padding: 5px 0;" align="left"><table style="margin: 0 0 0 10px;border-collapse:collapse;color: #444444; font-family: arial; font-size: 12px; border-color: #dddddd; background-color: #ffffff; "><tr><td width="580" style="vertical-align: top; padding: 5px 0; "><table cellpadding="0" cellspacing="0" style="border-collapse:collapse;width:565px;color: #444444; font-family: arial; font-size: 12px; border-color: #dddddd; background-color: #ffffff; " width="565"><tr><td style="padding:5px 0 5px 5px;line-height:normal;color: #444444; font-family: arial; font-size: 12px; border-color: #dddddd; background-color: #ffffff; ">';
    
        return $templateHtml;
    }
    public function getHtmlFooter(){
        $templateHtml = '</td></tr></table></td></tr></table></td>
			              </tr></table>
</td>
			        </tr></table>
<table cellpadding="0" cellspacing="0" border="0" height="10" style="height: 10px;border-collapse: collapse;font-size: 1px;"><tr><td height="10" style="height:10px; border-spacing: 0;font-size: 1px;"> </td></tr></table>
<table cellpadding="10" cellspacing="0" border="0" style="margin: 0; border-collapse: collapse;" width="600"><tr>
<td style="color: #444444; background-color: #eeeeee; border-color: #eeeeee; align: left; font-family: arial; font-size: 11px; ">
			      					<table cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 0; border-collapse: collapse;" width="100%"><tr>
<td align="left" valign="top" style="vertical-align: top; text-align: left; color: #444444; background-color: #eeeeee; border-color: #eeeeee; align: left; font-family: arial; font-size: 11px; ">
					<div id="nl_footer" style="display: block; text-align: left; margin: 0;">Cet email a été envoyé à <a href="mailto:[[EMAIL_TO]]" style="color:#000000;border: none;">[[EMAIL_TO]]</a>, <a href="[[UNSUB_LINK_FR]]" style="color:#000000;border: none;">cliquez ici pour vous désabonner</a>.</div>
					<div id="footer_address" style="display: block; text-align: left; margin: 5px 0 0;">
</div>
				</td>
				<td align="right" valign="top" style="vertical-align: top; text-align: right">
					
				</td>
		    	
			      						</tr></table>
</td>
							</tr></table>
</td>
			  </tr></table>
</body>
</html>';
        
        return $templateHtml;
    }
    
    public function getMessage($type='default'){
        $path = $this->_getTemplatePath($type);
        if(file_exists($path)){
            ob_start();
            $baseUrl = PWE::getBaseURL();
            include($path);
            $message = ob_get_contents();
            ob_end_clean();
            return str_replace('[[CONTENT]]',$this->getContent(),$message);
        }else{
            $html = $this->getHtmlHeader().$this->getContent().$this->getHtmlFooter();
            return $html;
        }
    }
    
    private static function _getTemplatePath($type='default'){
        $path = PWE::getThemePath().'/newsletter/'.$type.'.php';
        if(file_exists($path)){
            return $path;
        }else{
            return PWE::getThemePath().'/newsletter/default.php';
        }
    }
    
    public function getDatas($input=false){
        $data = array();
        $data['id'] = $this->getId();
        $data['title'] = $this->getTitle($input);
        $data['mailjet_id'] = $this->getMailjetId();
        $data['content'] = $this->getContent($input);
        $data['date_crea'] = $this->getDateCrea();
        $data['is_deleted'] = $this->getIsDeleted();
        return $data;
    }
    public function setDatas($datas) {
        foreach($datas as $k=>$v){
            if(substr($k, 0, 1) !== '_') $k = '_'.$k;
            if($k != '_id' && property_exists(get_class($this),$k)){
                $this->$k = $v;
            }
        }
        return true;
    }
    public function update(){
        $sqlQuery = 'UPDATE '.self::getTable().' SET ';
        $sqlQuery .= '`title`="'.$this->getTitle(true).'",';
        $sqlQuery .= 'is_deleted='.$this->getIsDeleted().',';
        $sqlQuery .= 'content="'.$this->getContent(true).'", ';
        $sqlQuery .= 'mailjet_id='.$this->getMailjetId().' ';
        $sqlQuery .= 'WHERE id='.$this->_id.' LIMIT 1';
        bdd_sql::doSql($sqlQuery,false);
        return array('success'=>true,'datas'=>$this->getDatas());
    }
    public function save(){
        $this->_date_crea = date('Y-m-d H:i:s');
        $sqlQuery = 'INSERT INTO '.self::getTable().' (mailjet_id,title,content,date_crea) VALUES (';
        $sqlQuery .= $this->getMailjetId().',';
        $sqlQuery .= '"'.$this->getTitle(true).'",';
        $sqlQuery .= '"'.$this->getContent(true).'",';
        $sqlQuery .= '"'.$this->getDateCrea().'") ';
        bdd_sql::doSql($sqlQuery,false);
        $this->_id = bdd_sql::getLastInsertId();

        return array('success'=>true,'datas'=>$this->getDatas());
    }
    public function delete() {
        $sqlDelete = 'UPDATE '.self::getTable().' SET is_deleted=1 WHERE id='.$this->_id;
        bdd_sql::doSql($sqlDelete,false);

        return array('success'=>true,'datas'=>array());
    }
    
    /* fonctions statiques */
    public static function getTable(){
        return PWE::getPrefixBdd().self::$_table;
    }
    
    public static function getListe($active=false){
        $sql = "SELECT id FROM ".self::getTable();
        if($active) $sql .= ' WHERE is_deleted=0';
        $results = bdd_sql::doSql($sql,true);
        $retour = array();
        if($results['count']>0){
            foreach($results['results'] as $newsletterData){
                $retour[] = new modele_newsletter($newsletterData['id']);
            }
        }
        return $retour;
    }
}
?>